package core.helpers;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

import core.Pilot;

/**
 * Class provides additional methods for specific actions on web elements
 */
public class ActionBuilderHelper
{
	protected static Pilot manager;
	private Actions builder;

	/**
	 * Constructor
	 * @param p_pilot
	 */
	public ActionBuilderHelper(Pilot p_pilot)
	{
		manager = p_pilot;
	}

	/**
	 * Moves mouse to element
	 * @param p_webElement
	 */
	public void mouseOver(WebElement p_webElement)
	{
		builder = new Actions(manager.driver().getDriver());
		builder.moveToElement(p_webElement)
				.build()
				.perform();
	}

	/**
	 * Double clicks to element
	 * @param p_webElement
	 */
	public void doubleClick(WebElement p_webElement)
	{
		builder = new Actions(manager.driver().getDriver());
		builder.doubleClick(p_webElement)
				.build()
				.perform();
	}

	/**
	 * Make right click to element
	 * @param onElement
	 */
	public void contextClick(WebElement onElement)
	{
		builder = new Actions(manager.driver().getDriver());
		builder.contextClick(onElement)
				.build()
				.perform();
	}

	/**
	 * Drags and drops someElement to otherElement
	 * @param someElement
	 * @param otherElement
	 */
	public void dragAndDrop(WebElement someElement, WebElement otherElement)
	{
		builder = new Actions(manager.driver().getDriver());
		builder.dragAndDrop(someElement, otherElement)
				.build()
				.perform();
	}

	/**
	 * Moves the mouse and click to a web elements by coordinates
	 * @param p_webElement
	 *            Web element instance
	 * @param p_xAxis
	 *            Coordinate by X axis
	 * @param p_yAxis
	 *            Coordinate by Y axis
	 */

	public void moveMouseAndClick(WebElement p_webElement, int p_xAxis, int p_yAxis)
	{
		builder = new Actions(manager.driver().getDriver());
		manager.log().debug("Move and click to a web element => xAxis=" + p_xAxis + "; yAxis=" + p_yAxis);

		try
		{
			builder.moveToElement(p_webElement, p_xAxis, p_yAxis).click().build().perform();
		}
		catch (Exception p_ex)
		{
			manager.log().warn("Cannot execute a method 'moveMouseAndClick': " + p_ex);
		}

	}

	/**
	 * Point mouse to someElement and click to otherElement
	 * @param someElement
	 *            Web element on which move the mouse
	 * @param otherElement
	 *            Web element on which click
	 */
	public void mouseOverAndClick(WebElement someElement, WebElement otherElement)
	{
		builder = new Actions(manager.driver().getDriver());
		manager.log().debug("Move mouse to web element " + someElement + "and click on " + otherElement);
		builder.moveToElement(someElement)
				.click(otherElement)
				.build()
				.perform();
	}

	/**
	 * Point mouse to someElement and click on it
	 * @param someElement
	 */
	public void mouseOverAndClick(WebElement someElement)
	{
		builder = new Actions(manager.driver().getDriver());
		builder.moveToElement(someElement)
				.click(someElement)
				.build()
				.perform();
	}

	/**
	 * Point mouse to someElement and sent some text to otherElement
	 * @param someElement
	 * @param otherElement
	 * @param text
	 */
	public void mouseOverAndSendKeys(WebElement someElement, WebElement otherElement, String text)
	{
		builder = new Actions(manager.driver().getDriver());
		builder.moveToElement(someElement)
				.sendKeys(otherElement, text)
				.build()
				.perform();
	}

	/**
	 * Point mouse to someElement and sent some text
	 * @param someElement
	 * @param text
	 */
	public void mouseOverAndSendKeys(WebElement someElement, String text)
	{
		builder = new Actions(manager.driver().getDriver());
		builder.moveToElement(someElement)
				.sendKeys(someElement, text)
				.build()
				.perform();
	}

}
